package com.unmarshaller.model;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import com.unmarshaller.UnmarshallerDerve;
import com.unmarshaller.annotation.FilePath;

@FilePath("configuration.xml")
@XmlRootElement(name = "unmarshaller_configuration")
@XmlAccessorType(XmlAccessType.NONE)
public class ConfigData
{
	@XmlElement(name = "configuration")
	List<ConfigModel> list;
	
	/*properties配置文件中的配置类读取*/
	List<Class<?>> propertiesLoads = new ArrayList<Class<?>>();
	/*xml配置文件中的配置类读取*/
	List<Class<?>> xmlLoads        = new ArrayList<Class<?>>();
	
	void afterUnmarshal(Unmarshaller u, Object parent) 
	{
		
		for (ConfigModel model : list) 
		{
			if(model.clazz == null)
			{
				UnmarshallerDerve.log.info("[ERROR]解析类文件路径有误！ ---[" + model.info + "]");
				continue;
			}
			
			if(model.type.equalsIgnoreCase("xml"))
			{
				xmlLoads.add(model.clazz);
			}
			else if(model.type.equalsIgnoreCase("property"))
			{
				propertiesLoads.add(model.clazz);
			}
			else
			{
				UnmarshallerDerve.log.info("[ERROR]解析类文件类型有误！ ---[" + model.info + "]");
				continue;
			}
		}
		
		list = null;
		
		UnmarshallerDerve.log.info("loading properties size[" + propertiesLoads.size() + "] xml size[" + xmlLoads.size() + "]");
	}

	public List<Class<?>> getPropertiesLoads() {
		return propertiesLoads;
	}

	public List<Class<?>> getXmlLoads() {
		return xmlLoads;
	}
}


@XmlRootElement(name = "configuration")
@XmlAccessorType(XmlAccessType.NONE)
class ConfigModel
{
	@XmlAttribute(name = "CLASS")
	Class<?> clazz;
	
	@XmlAttribute(name = "TYPE")
	String type;
	
	@XmlAttribute(name = "INFO")
	String info;
}



